package com.Nuyoah.demo01.service.serviceImpl;

import com.Nuyoah.demo01.DTO.CommunityDTO;
import com.Nuyoah.demo01.DTO.CommunityPageQry;
import com.Nuyoah.demo01.DTO.CommunityQry;
import com.Nuyoah.demo01.entity.Community;
import com.Nuyoah.demo01.mappers.CommunityMapper;
import com.Nuyoah.demo01.mappers.UserMapper;
import com.Nuyoah.demo01.service.CommunityService;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommunityImpl implements CommunityService {

    @Resource
    CommunityMapper communityMapper;
    @Resource
    UserMapper userMapper;
    public Integer createCommunity(CommunityDTO dto){
        return communityMapper.createCommunity(dto);
    }
    public Integer deleteCommunity(Integer communityId){
        return communityMapper.deleteCommunity(communityId);
    }
    public Integer updateCommunity(CommunityQry qry){
        return communityMapper.updateCommunity(qry);
    }
    public List<Community> selectAllCommunity(){
        return communityMapper.selectAllCommunity();
    }
    public List<Community> selectPageCommunity(CommunityPageQry qry) {
        PageHelper.startPage(qry.getPageNum(),qry.getPageSize());
        List<Community> Communities = communityMapper.selectPageCommunity(qry);
        //根据Community中的creatorId查询creatorName
        for (Community community : Communities) {
            community.setCreatorName(userMapper.selectByUserId(community.getCreatorId()).getUsername());
        }
        return  Communities;
    }

    /**
     * 根据id查询社区信息
     * @param communityId
     * @return
     */
    public Community selectCommunityById(Integer communityId) {
        return communityMapper.selectCommunityById(communityId);
    }

    /**
     * 根据用户id查询该用户创建的所有社区信息
     * @param userId
     * @return
     */
    public List<Community> selectCommunityByUserId(Integer userId) {
        return communityMapper.selectCommunityByUserId(userId);
    }
}
